//
//  BlockModel.m
//  CoreGraphicsTest
//
//  Created by aewatson on 11/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SpriteModel.h"


@implementation SpriteModel

@synthesize centerX;
@synthesize centerY;
@synthesize velocityX;
@synthesize velocityY;
@synthesize size;
@synthesize direction;
@synthesize color;
@synthesize rect;
@synthesize state;
@synthesize stateInterval;
@synthesize spriteType;
@synthesize isAffectedByGravity;

-(id)initWithSize:(float) newSize centerX:(float) newX centerY:(float) newY direction:(DIRECTION) newDirection andColor:(COLOR) newColor {
    self = [super init];
    
    // initialize datamembers
    [self setCenterX: newX];
    [self setCenterY: newY];
    [self setSize: newSize];
    [self setDirection: newDirection];
    [self setColor: newColor];
    [self setState:0];
    [self setStateInterval:0];
    // finished initializing
    return self;
}

-(id)initAsExplosionWithBlock:(SpriteModel*) block {
    self = [self initWithSize:EXPLOSION_SIZE centerX:[block centerX] centerY:[block centerY] direction:[block direction] andColor:[block color]];
    [self setSpriteType: TYPE_EXPLOSION];
    [self setIsAffectedByGravity:true];
    [self setState:1];

    [self setVelocityX:[block velocityX]];
    [self setVelocityY:[block velocityY]];
    return self;
}

-(id)initAsShotWithColor:(COLOR) newColor {
    self = [self initWithSize:SHOT_SIZE centerX:VIEW_WIDTH/2 centerY:(VIEW_HEIGHT) direction:UP andColor:newColor];
    [self setSpriteType:TYPE_SHOT];
    [self setIsAffectedByGravity:false];
    
    // initialize velocities
    // pixels per second
    [self setVelocityX:SHOT_SPEED_HORIZ];
    [self setVelocityY:SHOT_SPEED_VERT]; // pixels per second

    return self;
}

-(id)initAsBlockWithDirection:(DIRECTION) newDirection color:(COLOR) newColor velocityX:(int) newVelocityX andVelocityY:(int) newVelocityY {
    int startX;
    int startY = BLOCK_START_Y;
    switch (newDirection) {
        case LEFT:
            startX = VIEW_WIDTH - 1;
            break;
        case RIGHT:
            startX = 1;
            break;
        default:
            break;
    };
    self = [self initWithSize:BLOCK_SIZE centerX:startX centerY:startY direction:newDirection andColor:newColor];
    [self setSpriteType:TYPE_BLOCK];
    
    // initialize velocities
    [self setVelocityX:newVelocityX]; // pixels per second
    [self setVelocityY:newVelocityY]; // pixels per second
    [self setIsAffectedByGravity:true];

    return self;
}

-(void)isHit {
    
}

-(bool)nextState {
    // if time to change image
    if(stateInterval == STATE_INTERVAL) {
        // increment state
        state++;
        // reset interval
        stateInterval = 0;
        return true;
    }
    else {
        // otherwise, increment interval
        stateInterval++;
    }
    return false;
}

-(float)getLeft {
    return centerX - size / 2;
}

-(float)getTop {
    return centerY - size / 2;
}

-(float)getWidth{
    return (float) size;
}

-(float)getHeight{
    return (float) size;
}

-(COLOR) getColor {
    return color;
}

-(CGRect) getRect {
    return CGRectMake([self getLeft], [self getTop], [self getWidth], [self getHeight]);
}

-(void) setCenterX:(float) newX andY:(float) newY {
    [self setCenterX:newX];
    [self setCenterY:newY];
}

-(void) moveByDeltaX:(float) dX andDeltaY:(float) dY {
    centerX += dX;
    centerY += dY;
}

-(void) locationAfterMilliseconds:(float) ms {
    float seconds = ms / 1000;
    NSLog(@"CenterX was %0.2f", centerX);
    centerX += direction * velocityX * seconds;
    NSLog(@"CenterX is now %0.2f", centerX);
    centerY += velocityY * seconds;
    if([self isAffectedByGravity]) {
    //    centerY += 0.5 * ([GameModel getAcceleration] * (seconds * seconds));
        velocityY += [GameModel getAcceleration] * seconds;
    }
    
}

@end
